﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class CustomerPOList : System.Web.UI.Page
    {
        Helper helper = new Helper(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CustomerPOController customerPoController=new CustomerPOController();
                IList<CustomerPOEntity> customerPoEntities = customerPoController.FindAllCustomerPO();
                CustomerPOListGridView.DataSource = customerPoEntities;
                CustomerPOListGridView.DataBind();
            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void CustomerPOListGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if(e.CommandName=="Import")
            {
                GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string cid = ((HyperLink)row.FindControl("PONoHyperLink")).ToolTip;
                int custPOId = int.Parse(cid);
                CustomerPOItemController customerPoItemController = new CustomerPOItemController();
                IList<CustomerPOItemEntity> customerPoItemEntities =
                    customerPoItemController.findCustomerPOItemsByCustPOId(custPOId);
                IList<VendorPOItemEntity> vendorPoItemEntities=new List<VendorPOItemEntity>();
                
                for(int i=0;i<customerPoItemEntities.Count;i++)
                {
                    VendorPOItemEntity vendorPoItemEntity = new VendorPOItemEntity();
                    //construct the current customer po item entity
                    CustomerPOItemEntity customerPoItemEntity=new CustomerPOItemEntity();
                    customerPoItemEntity = customerPoItemEntities.ElementAt(i);
                    //construct product to get product name
                    ProductController productController=new ProductController();
                    int productId=customerPoItemEntities.ElementAt(i).ProductId;
                    ProductEntity productEntity = productController.FindProductById(productId);
                    vendorPoItemEntity.ProductId = customerPoItemEntity.ProductId;
                    vendorPoItemEntity.ProductName = productEntity.Name;
                    vendorPoItemEntity.ProductNo=productEntity.No;
                    vendorPoItemEntity.RequestQty = customerPoItemEntity.RequestQty;
                    vendorPoItemEntity.RequestDate = customerPoItemEntity.RequestDate;
                    vendorPoItemEntity.Price = customerPoItemEntity.Price;
                    vendorPoItemEntity.Lot = customerPoItemEntity.Lot;
                    vendorPoItemEntity.FormatDate = customerPoItemEntity.FormatDate;
                    vendorPoItemEntity.Rev = customerPoItemEntity.Revision;
                    vendorPoItemEntities.Add(vendorPoItemEntity);
                }
                Session["VendorPoItemEntities"] = vendorPoItemEntities;
                Response.Redirect("MakeVendorPO.aspx");
            }
        }

        protected void CustomerPOListGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            CustomerPOListGridView.PageIndex = e.NewPageIndex;
            CustomerPOController customerPoController = new CustomerPOController();
            IList<CustomerPOEntity> customerPoEntities = customerPoController.FindAllCustomerPO();
            CustomerPOListGridView.DataSource = customerPoEntities;
            CustomerPOListGridView.DataBind();

        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            string poNo = SearchTextBox.Text.Trim();
            CustomerPOController customerPoController = new CustomerPOController();
            IList<CustomerPOEntity> customerPoEntities = customerPoController.FindCustomerPOByNo(poNo);
            CustomerPOListGridView.DataSource = customerPoEntities;
            CustomerPOListGridView.DataBind();

        }

        protected void SearchButton1_Click(object sender, EventArgs e)
        {
            DateTime fromDate =Convert.ToDateTime(FromTextBox.Text.Trim());
            DateTime toDate = Convert.ToDateTime(ToSearchTextBox.Text.Trim());
            CustomerPOController customerPoController = new CustomerPOController();
            IList<CustomerPOEntity> customerPoEntities = customerPoController.FindCustomerPOByDate(fromDate, toDate);
            CustomerPOListGridView.DataSource = customerPoEntities;
            CustomerPOListGridView.DataBind();
        }
    }
}